package com.huiyi.quickproj.jundui.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

/**
 * describe: 处理工具类
 *
 * @author: robin
 * @email:  cuizhou6463@126.com
 * @date 2023-12-09
 */
public class MyUtils {


    public final static String DOUBLE_HORIZONTAL_BAR = "--";
    /**
     * 成员属性
     */
    public static Map<String, String> parse(String... args) {

        Map<String, String> parseMap = new HashMap(6);
        for (int i = 0; i < args.length; i++) {
            //检查是否是双横杠参数，非双横杠continue
            if (!args[i].contains(DOUBLE_HORIZONTAL_BAR)) {
                continue;
            }
            //获取双横杠参数对应数据
            String commandData = null;
            if (i + 1 < args.length && !args[i + 1].contains(DOUBLE_HORIZONTAL_BAR)) {
                commandData = args[i + 1];
            }
            //截取双横杠，获取参数名
            String parameter = args[i].substring(2);
            //存储参数、数据
            parseMap.put(parameter, commandData);
        }
        return parseMap;
    }

    public static String getExt(String fileName){
        int i = fileName.lastIndexOf(".");
        return fileName.substring(i+1, fileName.length());
    }

    public static String[] getColArrays(String prefix, int colNum){
        List<String> arr = new ArrayList<>();
        for(int i = 1;  i <= colNum; i++){
            arr.add(prefix + i);
        }
        return arr.toArray(new String[colNum]);
    }

    public static String[] getColArrays(String headStr, String defStr){
        if("|".equals(defStr)){
            defStr = "\\|";
        }
        String[] split = headStr.split(defStr);
        // 对表头作特殊处理保障 文件的ddl语句可执行
        List<String> splitList = new ArrayList<>();
        for(String s : split){
            String s1 = MyUtils.makePlainWithoutQuote(s);
            s1 = s1.replace("-","_");
            s1 = s1.replace(" ", "_");
            s1 = s1.replace("&", "_");
            s1 = s1.replace("/", "_");
            s1 = s1.replace("\\", "_");
            s1 = s1.replace("\"", "");
            if(s1.length() >= 31){
                s1 = s1.substring(0, 30);
                System.out.println( s1 );
            }
            splitList.add(s1);
        }
        return splitList.toArray(new String[splitList.size()-1]);
    }


    public static List<String> getDataArrays(String data, String delimiter, int colNum) throws Exception {
        if(StringUtils.isNotEmpty( data ) && !" ".equals(data) && !"\u0000".equals(data) && !" ".equals(data) && data.length() != 1){
            if("|".equals(delimiter)){
                delimiter = "\\|";
            }
            String[] s = data.split(delimiter);
            // 创建新数组，长度比原数组大 1
            String[] newArray = null;
            if(s.length < colNum){
                newArray = new String[s.length + (colNum-s.length)];
                // 复制原数组的元素到新数组
                System.arraycopy(s, 0, newArray, 0, s.length);
                for(int ii = 1; ii <= colNum-s.length ;ii++){
                    newArray[s.length + (ii-1)] = "";
                }

                List<String> list = Arrays.asList(newArray);
                return list;
            }
            //临时解决方案
//            if("1".equals("1")){ //special
//                String s1 = data.substring(0, data.indexOf(delimiter));
//                String s2 = data.substring(data.indexOf(delimiter)+1 );
//                List<String> _list = new ArrayList<>();
//                _list.add(s1);
//                _list.add(s2);
//                return _list;
//            }
            else if( s.length > colNum ){
                throw new Exception(data + " (too many values)");
            }
            else{
                List<String> list = Arrays.asList(s);
                return list;
            }
        }
        return null;
    }

    public static String getFileNameWithoutExt(String fileName){
        int i = fileName.indexOf(".");
        return fileName.substring(0, i);
    }

    public static Integer getMaxValue(Map<String, List<Integer>> aa, String key) {
        List<Integer> _v = aa.get(key);
        if (_v == null || _v.size() == 0) {
            throw new IllegalArgumentException("List不能为空");
        }

        // 初始化最大值为列表的第一个元素
        Integer maxValue = _v.get(0);

        // 遍历列表找到最大值
        for (int i = 1; i < _v.size(); i++) {
            if (_v.get(i) > maxValue) {
                maxValue = _v.get(i);
            }
        }

        return maxValue;
    }


    public static String makePlainWithoutQuote(String str){
        //"100022616385850"
        if(!StringUtils.isEmpty(str) &&
                str.startsWith("\"") && str.endsWith("\"") &&
                str.length() > 1){
            String result = str.substring(1, str.length() - 1);
            return result;
        }
        return str;
    }

    public static String getHeadLineWithFilePath(String filePath){
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            // 读取文件的第一行
            String firstLine = reader.readLine();
            if (firstLine != null) {
                return firstLine;
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getStrByLength(String inputStr, int length, String replaceStr){
        System.out.println(inputStr);
        String tmp =  inputStr.substring(0, length);
        tmp = tmp.replace(" ", "_");
        if(StringUtils.isNotEmpty(replaceStr)){
            tmp = tmp.replace(replaceStr, "");
        }
        return tmp;
    }

    public static void main(String[] args) throws InterruptedException {
        System.out.println( "ALLEGHENY FVE 20181001.txt".contains("Five") );
        String input  = "Collection  #1_BTC combos";

        System.out.println( getStrByLength(input, 14, "#") );

        String s00 = "bigthriller1990@yahoo.com:jkl:jk8";
        String s001 = s00.substring( 0,s00.indexOf(":") );
        String s002 = s00.substring( s00.indexOf(":")+1 );
        System.out.println(s001);
        System.out.println(s002);

        String s0 = "'AA";
        System.out.println( s0.replace("'", "''") );

        String fileName = "aaa.txt";
        String str      = getExt(fileName);
        String str2     = getFileNameWithoutExt(fileName);
        System.out.println(str);
        System.out.println(str2);

        System.out.println(String.join(",",getColArrays("v", 10)) );

        System.out.println(2000%100);

        String t = "jjj aaaa";
        System.out.println( t.indexOf("d1 ") );
        System.out.println( "--------------" );
        if( t.indexOf(" ") >= 0 ){
            String _t1 = t.replace(" ", "_");
            System.out.println(_t1);
        }

        // 创建一个HashMap
        Map<String, List<Integer>> hashMap = new HashMap<>();
        hashMap.put("key1", null);
        hashMap.put("key2", null);
        hashMap.put("key3", null);

        // 添加键值对
        for(String key : hashMap.keySet()){
            List<Integer> vtest = new ArrayList();
            vtest.add(0);
            vtest.add(1);
            vtest.add(2);
            hashMap.put(key, vtest);
        }

        // 指定的key
        String specifiedKey = "key2";

        for(int ii=0; ii<10; ii++){
            List<Integer> integers = hashMap.get(specifiedKey);
            int newi = getMaxValue(hashMap, specifiedKey) + 1;
            integers.add(newi);
            hashMap.put( specifiedKey, integers);
        }
        System.out.println(hashMap);

        System.out.println( makePlainWithoutQuote("\"https://www.facebook.com/100009296212637\"") );

        // 测试执行时间
        long startTime = System.currentTimeMillis();

        // 调用你的方法
        Thread.sleep(1200);

        long endTime = System.currentTimeMillis();

        long elapsedTime = endTime - startTime;
        System.out.println("方法执行时间: " + String.format("%.2f seconds", (elapsedTime)/1_000.0) + " 秒");

        String tolongStr = "PrimaryRunOffLG542011PartyVot2sadfsadf"; // ok PrimaryRunOffLG542011PartyVot2
        if(tolongStr.length() >= 31){
            tolongStr = tolongStr.substring(0, 30);
            System.out.println( tolongStr );
        }

    }

}
